﻿using AutoMapper;
using MediatR;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Domains.wbx;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.Beginning.Api.Application.Command.Common
{
    public class SearchEarBedroomFieldHandler : IRequestHandler<SearchEarBedroomFieldCommand, Result>
    {
        private readonly IMapper mapper;

        /// <summary>
        /// 猪只基本信息
        /// </summary>
        private readonly IBaseRepository<BasicInformation> _basicInformationrepository;

        /// <summary>
        /// 栋舍
        /// </summary>
        private readonly IBaseRepository<Bedrooms> _bedroomsRepository;

        /// <summary>
        /// 栏位
        /// </summary>
        private readonly IBaseRepository<Fields> _fieldsRepository;

        /// <summary>
        /// 品种
        /// </summary>
        private readonly IBaseRepository<Varieties> _varietiesRepository;

        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SearchEarBedroomFieldHandler> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="basicInformationrepository"></param>
        /// <param name="bedroomsRepository"></param>
        /// <param name="fieldsRepository"></param>
        /// <param name="logger"></param>
        public SearchEarBedroomFieldHandler(IMapper mapper,
            IBaseRepository<BasicInformation> basicInformationrepository,
            IBaseRepository<Bedrooms> bedroomsRepository,
            IBaseRepository<Fields> fieldsRepository,
            IBaseRepository<Varieties> varietiesRepository,
            ILogger<SearchEarBedroomFieldHandler> logger)
        {
            this.mapper = mapper;
            _basicInformationrepository = basicInformationrepository;
            _bedroomsRepository = bedroomsRepository;
            _fieldsRepository = fieldsRepository;
            _varietiesRepository = varietiesRepository;
            _logger = logger;
        }
        public async Task<Result> Handle(SearchEarBedroomFieldCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var basic = (from a in await _basicInformationrepository.GetAll()
                             join b in await _bedroomsRepository.GetAll() on a.BedroomId equals b.BedroomId
                             join c in await _fieldsRepository.GetAll() on a.FieldId equals c.FieldId
                             join d in await _varietiesRepository.GetAll() on a.VarietiesId equals d.VarietiesId
                             where a.BasicInforId == 0 || a.BasicInforId == request.BasicInforId
                             select new BasicDto()
                             {
                                 BasicInforId = a.BasicInforId,
                                 EarNumber = a.EarNumber,
                                 Gender = a.Gender,
                                 PigTypeId = a.PigTypeId,
                                 BedroomName = b.BedroomName,
                                 FieldName = c.FieldName,
                                 VarietiesId = d.VarietiesId,
                                 VarietiesName = d.VarietiesName
                             }).FirstOrDefault();
                result.Data = basic;
                result.Msg = "猪只档案查询栋舍栏位成功";
            }
            catch (Exception ex)
            {
                _logger.LogError("猪只档案查询栋舍栏位失败" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
